package com.alatus.web;

import com.alatus.constant.Constants;
import com.alatus.model.TUser;
import com.alatus.query.UserQuery;
import com.alatus.result.Result;
import com.alatus.service.UserService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;

@RestController
public class UserController {
    @Resource
    private UserService userService;

//    获取登录信息
    @GetMapping(value = "/api/login/info")
    public Result loginInfo(Authentication authentication){
        TUser tUser = (TUser) authentication.getPrincipal();
        return Result.OK(tUser);
    }

    //免登录验证
    //因为发送的请求过来首先会过filter那一关,能到这说明token验证都通过了,我们直接返回200即可
    @GetMapping(value = "/api/login/free")
    public Result freeLogin(){
        return Result.OK();
    }
//    查询用户列表,用户分页查询

    @PreAuthorize(value = "hasAuthority('user:list')")
    @GetMapping(value = "/api/users")
//    传递参数current,可传可不传,
    public Result userPage(@RequestParam(value = Constants.CURRENT,required = false) Integer current){
        if(current == null){
            current = 1;
        }
//        返回结果为PageInfo
        PageInfo<TUser> userByPage = userService.getUserByPage(current);
        return Result.OK(userByPage);
    }

    @PreAuthorize(value = "hasAuthority('user:view')")
    @GetMapping(value = "/api/user/{id}")
    public Result userDetail(@PathVariable(value = Constants.ID)Integer id){
        TUser tUser = userService.getUserById(id);
        return Result.OK(tUser);
    }


//    添加用户
    @PreAuthorize(value = "hasAuthority('user:add')")
    @PostMapping(value = "/api/user/add")
    public Result addUser(UserQuery userQuery, @RequestHeader(value = Constants.TOKEN_NAME)String token){
        userQuery.setToken(token);
        int result = userService.saveUser(userQuery);
        return result >= 1 ? Result.OK() : Result.FAIL();
    }


//    编辑用户
    @PreAuthorize(value = "hasAuthority('user:edit')")
    @PutMapping(value = "/api/user/edit")
    public Result editUser(UserQuery userQuery, @RequestHeader(value = Constants.TOKEN_NAME)String token){
        userQuery.setToken(token);
        int result = userService.updateUser(userQuery);
        return result >= 1 ? Result.OK() : Result.FAIL();
    }

    @PreAuthorize(value = "hasAuthority('user:delete')")
    @DeleteMapping(value = "/api/user/del/{id}")
    public Result delUser(@PathVariable(value = Constants.ID) Integer id, @RequestHeader(value = Constants.TOKEN_NAME)String token){
        int result = userService.delUserById(id);
        return result >= 1 ? Result.OK() : Result.FAIL();
    }

    @PreAuthorize(value = "hasAuthority('user:delete')")
    @DeleteMapping(value = "/api/user/batchDel")
    public Result batchDelUsers(@RequestParam(value = Constants.IDS) String ids, @RequestHeader(value = Constants.TOKEN_NAME)String token){
//        将我们的字符串拆分并放到list中作为元素
        List<String> idList = Arrays.asList(ids.split(","));
        int result = userService.delUsersByIds(idList);
        return result >= idList.size() ? Result.OK() : Result.FAIL();
    }

    @PreAuthorize(value = "hasAuthority('clue:view')")
    @GetMapping(value = "/api/user/activityOwner")
    public Result loadActivityOwner(){
        return Result.OK(userService.getOwnerList());
    }
}